package com.parfinex.study.p2.mod202;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;

public class XMLTest {
    public static void main(String[] args) throws DocumentException {
        //1.获取XML解析对象
        SAXReader reader = new SAXReader();
        //2.解析XML 获取文档对象
        Document document = reader.read("D:\\Code\\Java\\JDBC\\mod2_task\\src\\com\\parfinex\\study\\p2\\mod202\\book.xml");
        //3.获取根元素
        Element rootElement = document.getRootElement();
        System.out.println(rootElement);
        //4.获取根元素名称
//        System.out.println(rootElement.getName());
        List<Element> elements = rootElement.elements();
        for (Element element: elements) {
            System.out.print(element.getName() + "    ");
            List<Element> elements1 = element.elements();
            for (Element e : elements1) {
                System.out.print(e.getName() + "    ");
            }
            System.out.println();
            break;
        }
        //5.获取属性及元素值
        for (Element element: elements) {
            System.out.print(element.attributeValue("id") + "    ");
            List<Element> elements1 = element.elements();
//            for (Element e: elements1){
            System.out.print(element.elementText("name")+ "    " + element.elementText("author") + "    " + element.elementText("price") + "    " + element.elementText("body"));
            //}
            System.out.println();
        }
    }
}
